#include <boost/foreach.hpp>
#include "AttchCond.h"

#define foreach BOOST_FOREACH

using namespace kc_sf;

// --------------------------------------------------------- //

AttchCond::AttchCond()
: aContentTypeCondition( NULL ),
  aFilenameCondition( NULL ),
  aExtensionCondition( NULL ),
  aSizeCondition( NULL )
{
}

// --------------------------------------------------------- //

AttchCond::AttchCond( const AttchCond &cond )
: aContentTypeCondition( NULL ),
  aFilenameCondition( NULL ),
  aExtensionCondition( NULL ),
  aSizeCondition( NULL )
{
	if( cond.aContentTypeCondition != NULL )
		aContentTypeCondition = new AttchCTypeCond( *cond.aContentTypeCondition );

	if( cond.aFilenameCondition != NULL )
		aFilenameCondition = new AttchFilenameCond( *cond.aFilenameCondition );

	if( cond.aExtensionCondition != NULL )
		aExtensionCondition = new AttchFilenameCond( *cond.aExtensionCondition );

	if( cond.aSizeCondition != NULL )
		aSizeCondition = new AttchSizeCond( *cond.aSizeCondition );
}

// --------------------------------------------------------- //

AttchCond & AttchCond::operator=( const AttchCond &cond )
{
	if( this ==  &cond )
		return *this;

	if( aContentTypeCondition )
	{
		delete aContentTypeCondition;
		aContentTypeCondition = NULL;
	}
	if( aFilenameCondition )
	{
		delete aFilenameCondition;
		aFilenameCondition = NULL;
	}
	if( aExtensionCondition )
	{
		delete aExtensionCondition;
		aExtensionCondition = NULL;
	}
	if( aSizeCondition )
	{
		delete aSizeCondition;
		aSizeCondition = NULL;
	}

	if( cond.aContentTypeCondition != NULL )
		aContentTypeCondition = new AttchCTypeCond( *cond.aContentTypeCondition );

	if( cond.aFilenameCondition != NULL )
		aFilenameCondition = new AttchFilenameCond( *cond.aFilenameCondition );

	if( cond.aExtensionCondition != NULL )
		aExtensionCondition = new AttchFilenameCond( *cond.aExtensionCondition );

	if( cond.aSizeCondition != NULL )
		aSizeCondition = new AttchSizeCond( *cond.aSizeCondition );

	return *this;
}

// --------------------------------------------------------- //

AttchCond::~AttchCond()
{
	if( aContentTypeCondition )
		delete aContentTypeCondition;
	if( aFilenameCondition )
		delete aFilenameCondition;
	if( aExtensionCondition )
		delete aExtensionCondition;
	if( aSizeCondition )
		delete aSizeCondition;
}

// --------------------------------------------------------- //

Logic::Symbol * AttchCond::clone() const
{
	return new AttchCond( *this );
}

// --------------------------------------------------------- //

bool AttchCond::eval( const Message &msg )
{
	int hm = 0;

	foreach( Attachment *attachment, msg.mAttachments )
	{
		if( aContentTypeCondition )
			if( aContentTypeCondition->eval( *attachment ) )
			{
				if( evalHowMany(++hm))
					return true;
			}
			else
				return false;
		if( aFilenameCondition )
			if( aFilenameCondition->eval( *attachment ) )
			{
				if( evalHowMany(++hm))
					return true;
			}
			else
				return false;
		if( aExtensionCondition )
			if( aExtensionCondition->eval( *attachment ) )
			{
				if( evalHowMany(++hm))
					return true;
			}
			else
				return false;
		if( aSizeCondition )
			if( aSizeCondition->eval( *attachment ) )
			{
				if( evalHowMany(++hm))
					return true;
			}
			else
				return false;
	}

	return false;
}
